axios.defaults.withCredentials = true;

var app = new Vue({
    el:'#app',
    data:{
        userId:null,
        password:null,
    },
    methods:{
        login(){
            if (this.userId===''||this.password===''){
                alert("用户名或密码不可为空！");
            }

            axios.post('http://localhost:8888/login',
                {
                    userId:this.userId,
                    password:this.password,
                },
                {
                    credentials: 'include',
                    headers:{
                        // @RequestParam需要使用如下这种type进行数据传输
                        'Content-Type':'application/x-www-form-urlencoded',
                    },
                    // 这个其实是在模仿qs库对传输的data进行处理，也可以直接调用qs库
                    // 见 https://blog.csdn.net/u011332271/article/details/100515870
                    // https://blog.csdn.net/qq_43681948/article/details/102815325
                    transformRequest: [
                        function(data) {
                            let ret = "";
                            for (let it in data) {
                                ret +=
                                    encodeURIComponent(it) +
                                    "=" +
                                    encodeURIComponent(data[it]) +
                                    "&";
                            }
                            return ret;
                        }
                    ],
                }).then((response)=>{

            }).catch(function(error){
                console.log(error);
                alert(error.data);
            });
        },
        logout(){
            axios.get('http://localhost:8888/logout').then((response)=>{
                console.log(response);
            }).catch(function(error){
                console.log(error);
                alert(error.data);
            });
        },
    }
});



